#!/bin/bash

function print() {
    echo "$(date +"%Y/%m/%d %H:%M:%S")" "$1"
}

function main() {
    if ping -c 1 "$3" >/dev/null; then
        print "$2在线"
    else
        print "发现$2又卡死了，重启"

        # 判断是虚拟机还是lxc容器
        if qm status "$1" >/dev/null 2>&1; then
            rm -f "/var/lock/qemu-server/lock-${1}.conf"
            qm stop "$1" >/dev/null 2>&1
            sleep 1s
            qm start "$1" >/dev/null 2>&1
        else
            rm -f "/var/lock/lxc/pve-config-${1}.lock"
            pct stop "$1" >/dev/null 2>&1
            pct start "$1" >/dev/null 2>&1
        fi

        print "重启$2成功"
    fi
}

main "$1" "$2" "$3"